package com.ruoyi.transaction.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.transaction.domain.DTO.SpuSpecDTO;
import com.ruoyi.transaction.domain.VO.SpuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.transaction.domain.Spu;
import com.ruoyi.transaction.service.ISpuService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商品SPUController
 * 
 * @author ruoyi
 * @date 2025-08-07
 */
@RestController
@RequestMapping("/spu")
public class SpuController extends BaseController
{
    @Autowired
    private ISpuService spuService;

    /**
     * 查询商品SPU列表
     */
    @RequiresPermissions("transaction:spu:list")
    @GetMapping("/list")
    public TableDataInfo list(SpuVO spuVO)
    {
        startPage();
        List<SpuVO> list = spuService.selectSpuList(spuVO);
        return getDataTable(list);
    }

    /**
     * 导出商品SPU列表
     */
    @RequiresPermissions("transaction:spu:export")
    @Log(title = "商品SPU", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SpuVO spuVO)
    {
        List<SpuVO> list = spuService.selectSpuList(spuVO);
        ExcelUtil<SpuVO> util = new ExcelUtil<SpuVO>(SpuVO.class);
        util.exportExcel(response, list, "商品SPU数据");
    }

    /**
     * 获取商品SPU详细信息
     */
    @RequiresPermissions("transaction:spu:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(spuService.selectSpuById(id));
    }

    /**
     * 新增商品SPU
     */
    @RequiresPermissions("transaction:spu:add")
    @Log(title = "商品SPU", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Spu spu)
    {
        Spu insertedSpu = spuService.insertSpu(spu);
        if (insertedSpu != null) {
            return AjaxResult.success("新增成功", insertedSpu);
        } else {
            return AjaxResult.error("新增失败");
        }
    }

    /**
     * 修改商品SPU
     */
    @RequiresPermissions("transaction:spu:edit")
    @Log(title = "商品SPU", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Spu spu)
    {
        return toAjax(spuService.updateSpu(spu));
    }

    /**
     * 删除商品SPU
     */
    @RequiresPermissions("transaction:spu:remove")
    @Log(title = "商品SPU", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(spuService.deleteSpuByIds(ids));
    }



    // 获取所有商品信息及其规格及其规格值
    @GetMapping("/SpuWithSpecs")
    public AjaxResult getAllSpuWithSpecs() {
        return success(spuService.getAllSpuWithSpecs());
    }

    //  向spu和规格的关联表中插入数据
    @PostMapping("/addSpuSpec")
    public AjaxResult addSpuSpec(@RequestParam Long spuId, @RequestParam Long specId) {
        return toAjax(spuService.addSpuSpec(spuId, specId)) ;

    }

    /**
     * 获取SPU已关联的规格ID列表
     */
    @GetMapping("/specs/{spuId}")
    public AjaxResult getSpuSpecs(@PathVariable Long spuId) {
        List<Long> specIds = spuService.getSpuSpecIds(spuId);
        return AjaxResult.success(specIds);
    }
    /**
     * 删除SPU的所有规格关联
     */
    @DeleteMapping("/specs/delete")
    public AjaxResult deleteSpuSpecs(@RequestParam Long spuId) {
        return toAjax(spuService.deleteSpuSpecs(spuId));
    }


}
